package dao;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import business.DefiHistorique;


public class DaoDefiHistorique {
	/* ------------------------------------------------ REQUETES PRECOMPILEES */
	private static CallableStatement statementGetDefisHistoriqueFaits = null;
	private static CallableStatement statementGetDefisHistoriqueRecus = null;


	/* --------------------------------------------------------- CONSTRUCTEUR */
	static {
		try {
			statementGetDefisHistoriqueFaits = DbConnection.getInstance().prepareCall("{call getDefisHistoriqueFaits(?)}");
			statementGetDefisHistoriqueRecus = DbConnection.getInstance().prepareCall("{call getDefisHistoriqueRecus(?)}");
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
	}


	/* -------------------------------------------------------------- METHODE */
	public static List<DefiHistorique> getDefisHistoriqueFaits(int id_utilisateur) {
		List<DefiHistorique> defis = null;
		
		try {
			ResultSet rs = null;
			
			synchronized(statementGetDefisHistoriqueFaits) {
				statementGetDefisHistoriqueFaits.setInt(1, id_utilisateur);
				
				rs = statementGetDefisHistoriqueFaits.executeQuery();
			}
			
			if(rs != null) {
				defis = new ArrayList<DefiHistorique>();
				
				while(rs.next()) {
					defis.add(new DefiHistorique(
							rs.getInt("id_defiHistorique"),
							rs.getInt("fk_id_demandeur"),
							rs.getInt("fk_id_cible"),
							rs.getInt("fk_id_pariHistoriqueDemandeur"),
							rs.getInt("fk_id_pariHistoriqueCible"),
							rs.getInt("etat")));
				}
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		
		return defis;
	}


	public static List<DefiHistorique> getDefisHistoriqueRecus(int id_utilisateur) {
		List<DefiHistorique> defis = null;
		
		try {
			ResultSet rs = null;
			
			synchronized(statementGetDefisHistoriqueRecus) {
				statementGetDefisHistoriqueRecus.setInt(1, id_utilisateur);
				
				rs = statementGetDefisHistoriqueRecus.executeQuery();
			}
			
			if(rs != null) {
				defis = new ArrayList<DefiHistorique>();
				
				while(rs.next()) {
					defis.add(new DefiHistorique(
							rs.getInt("id_defiHistorique"),
							rs.getInt("fk_id_demandeur"),
							rs.getInt("fk_id_cible"),
							rs.getInt("fk_id_pariHistoriqueDemandeur"),
							rs.getInt("fk_id_pariHistoriqueCible"),
							rs.getInt("etat")));
				}
			}
		}
		catch(SQLException e) {
			e.printStackTrace();
		}
		
		return defis;
	}
}
